﻿/* ----------------------------------------------------------------------------
 * Copyright (c) Philip Stears 
 *
 * This source code is subject to terms and conditions of the Microsoft Public 
 * License (Ms-PL).
 * 
 * By using this source code in any fashion, you are agreeing to be bound 
 * by the terms of the Microsoft Public License.
 * 
 * To request a copy of the Microsoft Public License, send a request by email
 * to license@connectbasic.net.
 *
 * This notice must not be removed from this software.
 * ---------------------------------------------------------------------------- */

namespace ConnectBasic.ObjectModel
{
    using System.Diagnostics;
    using ConnectBasic.Execution;
    using Microsoft.Scripting;
    using DlrExpression = Microsoft.Linq.Expressions.Expression;

    /// <summary>
    /// Represents a statement which calls a method.
    /// </summary>
    [DebuggerDisplay("Call {Call.TargetName}")]
    public class CallStatement : StatementNodeBase
    {
        private CallExpression mCall;
        private SourceSpan mLineSpan;

        /// <summary>
        /// Initializes an instance of the <see cref="CallStatement" /> class.
        /// </summary>
        /// <param name="lineSpan">The span in the source text which the call was parsed from.</param>
        /// <param name="call">The expression which defines the method to call.</param>
        public CallStatement(SourceSpan lineSpan, CallExpression call)
        {
            mLineSpan = lineSpan;
            mCall = call;
        }

        /// <summary>
        /// Gets  the span in the source text from which the call was parsed.
        /// </summary>
        public SourceSpan LineSpan
        {
            get { return mLineSpan; }
        }

        /// <summary>
        /// Gets the expression which defines the method to call.
        /// </summary>
        public CallExpression Call
        {
            get { return mCall; }
        }

        /// <inheritdoc />
        public override DlrExpression Compile(BasicCodeGenerator gen)
        {

            // Compile the expression and surround it in a block to lose its
            // return value
            DlrExpression call = DlrExpression.Block(typeof(void), mCall.Compile(gen));

            // Add debugging information
            call = gen.AddDebugInfo(call, mLineSpan);

            return call;
        }
    }
}
